import 'package:flutter/material.dart';
import '../../model/Bus.dart';
import '../../res.dart';
import '../../utils/color/ColorUtils.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import '../../config/Constant.dart';
import '../../model/Sort.dart';
import '../../utils/EventBus/EventBus.dart';

class TypeDialog extends StatefulWidget {
  TypeDialog({Key? key, required this.types, required this.setType})
      : super(key: key);
  List<String> types;
  Function(String type) setType;

  @override
  State<TypeDialog> createState() => _SortDialogState();
}

class _SortDialogState extends State<TypeDialog> {
  int index_ = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    setSelect(SpUtil.getInt(Constant.sort)!);
    setState(() {});
  }

  void setSelect(int index) {
    index_ = index;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(12)),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            margin: const EdgeInsets.only(bottom: 10),
            child: Row(
              children: [
                TextView("Select conversion format",
                    style: TextStyle(
                        color: ColorUtils.text,
                        fontSize: 18,
                        fontWeight: FontWeight.bold)),
                const Spacer(),
                MyLocalImage(
                  Res.sys_icon_delete_grey3x,
                  width: 20,
                  height: 20,
                  onclick: () {
                    Get.back();
                  },
                ),
              ],
            ),
          ),
          Expanded(
              child: ListView.builder(
            itemBuilder: (c, index) {
              return InkWell(
                onTap: () {
                  setSelect(index);
                },
                child: Column(
                  children: [
                    const Divider(),
                    Container(
                      height: 62,
                      alignment: Alignment.center,
                      child: Row(
                        children: [
                          TextView(
                            widget.types[index],
                            alignment: Alignment.centerLeft,
                            style:
                                TextStyle(color: ColorUtils.text, fontSize: 16),
                          ),
                          const Spacer(),
                          if (index_ == index)
                            const MyLocalImage(
                              Res.list_icon_select_check_red,
                              width: 20,
                              height: 20,
                            ),
                        ],
                      ),
                    ),
                  ],
                ),
              );
            },
            itemCount: widget.types.length,
          )),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextView(
                "CANCLE",
                width: 140,
                height: 45,
                alignment: Alignment.center,
                backcolor: rgba(247, 247, 247, 1),
                radius: BorderRadius.circular(6),
                style: TextStyle(
                    color: ColorUtils.text_, fontWeight: FontWeight.bold),
              ),
              const SizedBox(
                width: 16,
              ),
              TextView(
                "OK",
                width: 140,
                height: 45,
                alignment: Alignment.center,
                radius: BorderRadius.circular(6),
                backcolor: rgba(255, 78, 77, 1),
                style: const TextStyle(
                    color: Colors.white, fontWeight: FontWeight.bold),
                onclick: () {
                  Get.back();
                  // EventBusUtil.fireEvent(
                  //     Bus(Constant.SORT_MSG, SpUtil.getInt(Constant.sort)));
                  widget.setType(widget.types[index_]);
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}
